package lc.activiti.common.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import lc.activiti.common.HttpRequestStatus;
import lc.activiti.common.LCExceptionUtils;
import lc.activiti.common.Result;
import lc.activiti.common.service.ICommonApprovalService;
import lc.activiti.common.service.ICommonProcessService;
import lc.activiti.model.SubApprovalModel;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("/commonService")
public class CommonApprovalController {
	@Autowired
	private ICommonApprovalService commonApprovalService;
	/**
	 * 提交订单
	 */
	@PostMapping("/submitApproval")
	public Result<Object> submitApply(@RequestBody SubApprovalModel appModel){
		Result<Object> result=new Result<>();
		try {
			commonApprovalService.submitApply(appModel);
			result.setMessage(HttpRequestStatus.Sucess.getDesc());
			result.setStatus(HttpRequestStatus.Sucess.getStatus());
			return result;
		} catch (Exception e) {
			result.setMessage(LCExceptionUtils.getExpetionMessage(e));
			result.setStatus(HttpRequestStatus.Failed.getStatus());
			e.printStackTrace();
			log.error("【通用工作流】提交订单错误",e);
			return result;
		}
	}
	/**
	 * 审核通过
	 */
	@PostMapping("/auditSuccess")
	public Result<Object> audtiSuccess(@RequestBody SubApprovalModel appModel){
		Result<Object> result=new Result<>();
		try {
			commonApprovalService.auditSuccess(appModel);
			result.setMessage(HttpRequestStatus.Sucess.getDesc());
			result.setStatus(HttpRequestStatus.Sucess.getStatus());
			return result;
		} catch (Exception e) {
			result.setMessage(LCExceptionUtils.getExpetionMessage(e));
			result.setStatus(HttpRequestStatus.Failed.getStatus());
			e.printStackTrace();
			log.error("【通用工作流】审核订单错误",e);
			return result;
		}
	}
	/**
	 * 审核驳回
	 */
	@PostMapping("/auditReject")
	public Result<Object> auditReject(@RequestBody SubApprovalModel appModel){
		Result<Object> result=new Result<>();
		try {
			commonApprovalService.auditReject(appModel);
			result.setMessage(HttpRequestStatus.Sucess.getDesc());
			result.setStatus(HttpRequestStatus.Sucess.getStatus());
			return result;
		} catch (Exception e) {
			result.setMessage(LCExceptionUtils.getExpetionMessage(e));
			result.setStatus(HttpRequestStatus.Failed.getStatus());
			e.printStackTrace();
			log.error("【通用工作流】审核订单错误",e);
			return result;
		}
	}
	/**
	 * 审批通过
	 */
	@PostMapping("/approvalSuccess")
	public Result<Object> approvalSuccess(@RequestBody SubApprovalModel appModel){
		Result<Object> result=new Result<>();
		try {
			commonApprovalService.approvalSuccess(appModel);
			result.setMessage(HttpRequestStatus.Sucess.getDesc());
			result.setStatus(HttpRequestStatus.Sucess.getStatus());
			return result;
		} catch (Exception e) {
			result.setMessage(LCExceptionUtils.getExpetionMessage(e));
			result.setStatus(HttpRequestStatus.Failed.getStatus());
			e.printStackTrace();
			log.error("【通用工作流】审核订单错误",e);
			return result;
		}
	}
	/**
	 * 审批通过
	 */
	@PostMapping("/approvalReject")
	public Result<Object> approvalReject(@RequestBody SubApprovalModel appModel){
		Result<Object> result=new Result<>();
		try {
			commonApprovalService.approvalReject(appModel);
			result.setMessage(HttpRequestStatus.Sucess.getDesc());
			result.setStatus(HttpRequestStatus.Sucess.getStatus());
			return result;
		} catch (Exception e) {
			result.setMessage(LCExceptionUtils.getExpetionMessage(e));
			result.setStatus(HttpRequestStatus.Failed.getStatus());
			e.printStackTrace();
			log.error("【通用工作流】审核订单错误",e);
			return result;
		}
	}
	@Autowired
	private ICommonProcessService commonProcessService;
	/**
	 * 部署流程
	 */
	@RequestMapping("/deployCommonProcess")
	public Result<Object> deployCommonProcess(){
		Result<Object> result=new Result<>();
		try {
			commonProcessService.deployCommonProcess();
			result.setMessage("工作流部署成功!");
			result.setStatus(HttpRequestStatus.Sucess.getStatus());
			return result;
		} catch (Exception e) {
			result.setMessage(LCExceptionUtils.getExpetionMessage(e));
			result.setStatus(HttpRequestStatus.Failed.getStatus());
			e.printStackTrace();
			log.error("【通用工作流】部署流程错误",e);
			return result;
		}
	}
}
